<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Baoming extends Controller
{
    public function index()
    {
        $res = $this->act();
        $course = Db::name('course')
            ->field(['id', 'coursename'])
            ->select();
        $acid = input('id');
        $refertel = Session::get('refertel');
        if (empty($acid)) {
            $data = [
                'course' => $course,
                'actname' => $res,
                'refertel' => $refertel
            ];
        } else {
            $data = [
                'course' => $course,
                'actname' => $acid,
                'refertel' => $refertel
            ];
        }
        return view('', $data);
    }

    public function act()
    {
        $res = Db::name('activity')
            ->field(['id', 'name'])
            ->select();
//        $aid = [];
//        $aname = [];
//        foreach ($res as $v){
//            $aid[] = $v['id'];
//            $aname[] = $v['name'];
//        }
        return $res;
    }

    public function getclassid()
    {
        $courseid = input('courseid');
        $coursemoney = Db::name('course')
            ->field('price')
            ->where('id', $courseid)->find();
        $class = Db::name('classdate')
            ->field(['id', 'classname'])
            ->where('coid', $courseid)
            ->select();
        $arr = ['money' => $coursemoney, 'class' => $class];
        return $arr;
    }

    public function dobaoming(Request $request)
    {
        $baoming = $request->post();
        $res = Db::name('vip')
            ->where('utel', $baoming['mytel'])
            ->find();
        if (empty($res)) {
            $data = [
                'utel' => $baoming['mytel'],
                'upass' => md5($baoming['mytel']),
            ];
            $userid = Db::name('vip')
                ->insertGetId($data);
            $info = ['uid' => $userid, 'utel' => $baoming['mytel'], 'upass' => md5($baoming['mytel'])];
            Session::set('user_info', $info);
            $baoming = [
                'uid' => $userid,
                'courseid' => $baoming['course'],
                'classid' => $baoming['class'],
                'refertel' => $baoming['reftel'],
                'actid' => $baoming['activity'],
                'fee' => $baoming['fee'],
            ];
            $insertBM = Db::name('baoming')->insert($baoming);
            echo "<script>alert('报名成功，登录密码是您的手机号哦，请到个人中心修改密码');location='" . url('index/Person/index') . "'</script>";
        } else {
            $userid = $res['uid'];
            $info = ['uid' => $userid, 'utel' => $baoming['mytel'], 'upass' => md5($baoming['mytel'])];
            Session::set('user_info', $info);
            $unique = Db::name('baoming')
                ->where('courseid', $baoming['course'])
                ->where('classid', $baoming['class'])
                ->where('uid', $userid)
                ->select();
            if (!empty($unique)) {
                echo "<script>alert('您已报名该课程，不可重复报名哦');location='" . url('index/Baoming/index') . "'</script>";
                die;
            }
            $baoming = [
                'uid' => $userid,
                'courseid' => $baoming['course'],
                'classid' => $baoming['class'],
                'refertel' => $baoming['reftel'],
                'actid' => $baoming['activity'],
                'fee' => $baoming['fee']
            ];
            $insertBM = Db::name('baoming')->insert($baoming);
            $this->success('报名成功', url('index/Index/index'));
        }
    }

    public function doMoney()
    {
        $actid = input('act');
        $courseid = input('course');
        $act = Db::name('activity')
            ->field(['concession', 'reward'])
            ->where('id', $actid)
            ->find();
        $course = Db::name('course')
            ->field('price')
            ->where('id', $courseid)
            ->find();
        $money = $course['price'] - $act['concession'];
        $arr = ['yuanjia' => $course['price'], 'youhui' => $act['concession'], 'yingfu' => $money, 'rewards' => $act['reward']];
        return $arr;
    }

    public function getActivityMoney()
    {
        $activityid = input('myactivity');
        $actMoney = Db::name('activity')
            ->field('concession')
            ->where('id', $activityid)
            ->find();
//        dump($actMoney);
        return $actMoney;
    }
}
